package com.ruoyi.web.controller.loan.controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.loan.dto.*;
import com.ruoyi.loan.entity.User;
import com.ruoyi.loan.service.*;
import com.ruoyi.loan.utils.FileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.mail.Multipart;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author coderFu
 * @create 2022/4/5
 */
@Controller
@Api(value = "userController", tags = "用户前台模块")
@RequestMapping(value = "/loan/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private AdminStudentService adminStudentService;

    @Resource
    private HttpServletRequest request;

    @Autowired
    private ImageService imageService;

    @Autowired
    private ReturnLoanService returnLoanService;

    @Autowired
    private FileService fileService;

    @Autowired
    private FileUtils fileUtils;

    @Autowired
    private HttpServletRequest httpServletRequest;

    @GetMapping()
    public String message() {
        String prefix = "loan/user";
        return prefix + "/user";
    }

    @ResponseBody
    @ApiOperation("用户修改密码")
    @PostMapping("/updatePassword")
    public AjaxResult updatePassword(@RequestParam("password") String password) {
        HttpSession session = httpServletRequest.getSession();
        Object attribute = session.getAttribute(session.getId());
        User user = (User) attribute;
        Integer userId = user.getId();
        userService.updatePassword(userId,password);
        return AjaxResult.success();
    }

    @ResponseBody
    @ApiOperation("我的贷款记录")
    @GetMapping("/getMyLoanRecord")
    public AjaxResult getMyLoanRecord(@RequestParam("pageSize") int pageSize,
                                      @RequestParam("pageNum") int pageNum) {
        return AjaxResult.success(userService.getMyLoanRecord(pageNum, pageSize));
    }

    @ResponseBody
    @ApiOperation("根据贷款记录id获取贷款详情")
    @GetMapping("/getLoanRecordDetailsById")
    public AjaxResult getLoanRecordDetailsById(@RequestParam("id") int id) {
        return AjaxResult.success(userService.getLoanRecordDetails(id));
    }

    @ResponseBody
    @ApiOperation("贷款申请")
    @PostMapping("/loanApply")
    public AjaxResult loanApply(@RequestBody LoanApplyDto loanApplyDto) {
        userService.loanApply(loanApplyDto);
        return AjaxResult.success();
    }

    @ResponseBody
    @ApiOperation("修改个人信息")
    @PutMapping("/updateUserMessage")
    public AjaxResult updateUserMessage(@RequestBody UpdateUserMessageDto updateUserMessageDto) {
        System.out.println("updateUserMessageDto = " + updateUserMessageDto);
        userService.updateUserMessage(updateUserMessageDto);
        return AjaxResult.success();
    }

    @ResponseBody
    @ApiOperation("查询用户信息")
    @GetMapping("/getUserMessage")
    public AjaxResult getUserMessage() {
        HttpSession session = request.getSession();
        User user = null;
        Object attribute = session.getAttribute(session.getId());
        if (attribute == null) {
            throw new RuntimeException("登录失效，请重新登录");
        }
        user = (User) attribute;
        Integer userId = user.getId();
        User user1 = userService.getById(userId);
        String avatarUrl = user1.getAvatarUrl();
        if (avatarUrl != null){
            String showFilePath = fileUtils.getShowFilePath(avatarUrl);
            user1.setAvatarUrl(showFilePath);
        }
        return AjaxResult.success(user1);
    }

    @ResponseBody
    @ApiOperation("查询用户列表")
    @GetMapping("/userList")
    public AjaxResult selectUserList(SelectUserListDto selectUserListDto) {
        PageInfo pageInfo = userService.selectUserList(selectUserListDto);
        return AjaxResult.success(pageInfo);
    }

    @ResponseBody
    @ApiOperation("查询我的贫困生申请记录列表")
    @GetMapping("/myPoorApplyList")
    public AjaxResult myPoorApplyList() {
        HttpSession session = request.getSession();
        User user = null;
        Object attribute = session.getAttribute(session.getId());
        if (attribute == null) {
            throw new RuntimeException("登录失效，请重新登录");
        }
        user = (User) attribute;
        Integer userId = user.getId();
        PageInfo pageInfo = userService.myPoorApplyList(userId);
        return AjaxResult.success(pageInfo);
    }

    @ResponseBody
    @ApiOperation(value = "重置学生密码,为身份证后9位", notes = "重置学生密码")
    @RequestMapping(value = "/resetStudentPassword", method = RequestMethod.POST)
    public AjaxResult resetStudentPassword(String idCard) {
        adminStudentService.resetStudentPassword(idCard);
        return AjaxResult.success("已重置学生密码为身份证后9位");
    }

    @ResponseBody
    @ApiOperation(value = "上传贫困照片")
    @RequestMapping(value = "/uploadPoorImages", method = RequestMethod.POST)
    public AjaxResult uploadPoorImages(@RequestParam("files") MultipartFile[] files) {
        return AjaxResult.success(imageService.uploadPoorImages(files));
    }

    @ResponseBody
    @ApiOperation(value = "还款")
    @RequestMapping(value = "/repayment", method = RequestMethod.POST)
    public AjaxResult repayment(Integer loanId, int money) {
        returnLoanService.repayment(loanId, money);
        return AjaxResult.success();
    }

    @ResponseBody
    @ApiOperation(value = "上传用户头像")
    @RequestMapping(value = "/uploadAvaatr", method = RequestMethod.POST)
    public AjaxResult repayment(MultipartFile file) {
        String path = fileService.uploadFile(file,FileUtils.AVATAR);
        return AjaxResult.success(path);
    }

    @ResponseBody
    @ApiOperation("查询贷款的还款记录")
    @RequestMapping(value = "/getLoanReturnRecords", method = RequestMethod.GET)
    public AjaxResult getLoanReturnRecords(@RequestParam("loanId") Integer loanId){
        return AjaxResult.success(returnLoanService.getLoanReturnRecords(loanId));
    }

}
